package com.sharkpro.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharkpro.constants.RedisConstant;
import com.sharkpro.constants.SaltConstant;
import com.sharkpro.entity.SysUser;
import com.sharkpro.mapper.SysUserMapper;
import com.sharkpro.service.ImageService;
import com.sharkpro.service.SysUserService;
import com.sharkpro.utils.RedisUtil;
import com.sharkpro.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author jianxin.wei
 * @description 针对表【sys_user(用户)】的数据库操作Service实现
 * @createDate 2024-12-16 16:05:13
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    @Resource
    SysUserMapper userMapper;
    @Resource
    RedisUtil redisUtil;

//    @Override
//    public ResultUtil login(String account, String password) {
//        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account", account).eq("password", password);
//        SysUser sysUser = userMapper.selectOne(queryWrapper);
//        if (sysUser != null) {
//            return new ResultUtil().success(sysUser);
//        }
//        return new ResultUtil().fail("账号或密码错误");
//    }

    @Override
    public ResultUtil list(String admin) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin", admin);
        List<SysUser> list = userMapper.selectList(queryWrapper);
        return new ResultUtil().success(list);
    }

    @Override
    public void export(HttpServletResponse response) {
        // 创建数据行
        List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
        List<String> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1");
        List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2");
        List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
        List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");

        List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);

        // 通过工具类创建writer
        try (ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx")) {
            // 跳过当前行，既第一行，非必须，在此演示用
            writer.passCurrentRow();

            // 合并单元格后的标题行，使用默认标题样式
            writer.merge(row1.size() - 1, "测试标题");

            // 一次性写出内容，强制输出标题
            writer.write(rows, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ResultUtil logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        redisUtil.removeMapCache(RedisConstant.RPKEY_SECURITY_TOKEN , "token:"+token);
        return new ResultUtil().fail("操作成功");
    }


}




